package cn.earas.ip.netdata;

import java.io.IOException;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.earas.ip.Data;

public class IPGet_89 implements Runnable
{
    private void getIP(CloseableHttpClient httpClient) {
        final HttpGet get = new HttpGet("http://www.89ip.cn/tqdl.html?api=1&num=9999&port=&address=&isp=");
        get.setHeader("Host", "www.89ip.cn");
        get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
        get.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        get.setHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        get.setHeader("Accept-Encoding", "gzip, deflate");
        get.setHeader("Connection", "keep-alive");
        get.setHeader("Cookie", "yd_cookie=3a0fa91f-9440-49792f2b03eb828136a7aca9cf55f9e80948");
        try {
            final HttpEntity entity = httpClient.execute((HttpUriRequest)get).getEntity();
            String result = EntityUtils.toString(entity);
            result = result.substring(result.lastIndexOf("</script>") + 9);
            if (result.contains("<br>")) {
            	Pattern p = Pattern.compile(Data.REG);
                for (final String data : result.split("<br>")) {
                	if(!p.matcher(data).matches()) continue;
                	if(!Data.ips.contains(data)) Data.ips.add(data);
                }
            }
        }
        catch (ClientProtocolException e2) {
            System.out.println("89IP\u83b7\u53d6\u5931\u8d25\uff01");
        }
        catch (IOException e3) {
            System.out.println("89IP\u83b7\u53d6\u5931\u8d25\uff01");
        }
    }
    
    private void getIP2(CloseableHttpClient httpClient, String url) {
        final HttpGet get = new HttpGet(url);
        get.setHeader("Host", "www.89ip.cn");
        get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
        get.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        get.setHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        get.setHeader("Accept-Encoding", "gzip, deflate");
        get.setHeader("Connection", "keep-alive");
        get.setHeader("Cookie", "yd_cookie=3a0fa91f-9440-49792f2b03eb828136a7aca9cf55f9e80948");
        try {
            final HttpEntity entity = httpClient.execute((HttpUriRequest)get).getEntity();
            String result = EntityUtils.toString(entity);
            Document doc = Jsoup.parse(result);
            Elements trs = doc.getElementsByClass("layui-table")
            		.get(0).getElementsByTag("tbody").get(0)
            		.getElementsByTag("tr");
            for(Element tr : trs){
            	String ip = tr.getElementsByTag("td").get(0).text().trim();
            	String port = tr.getElementsByTag("td").get(1).text().trim();
            	if(!Data.ips.contains(ip + ":" + port)) Data.ips.add(ip + ":" + port);
            }
        }
        catch (ClientProtocolException e2) {
            System.out.println("89IP\u83b7\u53d6\u5931\u8d25\uff01");
        }
        catch (Exception e3) {
            System.out.println("89IP\u83b7\u53d6\u5931\u8d25\uff01");
        }
    }
    
    @Override
    public void run() {
    	CloseableHttpClient httpClient = HttpClients.createDefault();
        this.getIP(httpClient);
        for(int i=1;i<=10;i++){
        	if(!Data.run) break;
        	getIP2(httpClient,"http://www.89ip.cn/index_"+i+".html");
        }
        try {
			if(httpClient != null) httpClient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
